﻿namespace NET7.Infrastructure.Tools;

/// <summary>
/// 当前用户
/// </summary>
public static class CurrentUser
{
    public static string UserId
    {
        get
        {
            var id = BuilderExtensions.ServiceProvider.GetRequiredService<IHttpContextAccessor>()?.HttpContext?.User
                ?.FindFirst(MyClaims.UserId);
            if (id != null)
            {
                return id.Value;
            }

            return string.Empty;
        }
    }

    public static string UserName
    {
        get
        {
            var name = BuilderExtensions.ServiceProvider.GetRequiredService<IHttpContextAccessor>()?.HttpContext?.User
                ?.FindFirst(MyClaims.UserName);
            if (name != null)
            {
                return name.Value;
            }

            return string.Empty;
        }
    }
}

/// <summary>
/// 自定义JWT的Claim
/// </summary>
public static class MyClaims
{
    /// <summary>
    /// 用户Id
    /// 参考系统Claim: <see cref="T:System.Security.Claims.ClaimTypes" /> 
    /// </summary>
    public const string UserId = "user_id";

    /// <summary>
    /// 用户名
    /// </summary>
    public const string UserName = "user_name";

    /// <summary>
    /// 部门
    /// </summary>
    public const string Department = "department";

    /// <summary>
    /// 分组
    /// </summary>
    public const string Group = "xxx";

    /// <summary>
    /// 分组
    /// </summary>
    public const string Role = "role";
}